//
//  PWInputView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/28.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol PWInputViewDelegate {
    @objc optional func checkInputText() //检查输入内容
}

class PWInputView: UIView {

    weak var inputDelegate : PWInputViewDelegate?
    
    fileprivate let titleLabel = UILabel()
    let textView = UITextField()
    
    var text : String! {
        didSet{
            titleLabel.text = text
        }
    }
    
    var placeholder : String! {
        didSet{
            textView.placeholder = placeholder
        }
    }
    
    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.white
        
        self.layer.borderWidth = 1
        self.layer.borderColor = UIColor.lineColor().cgColor
        
        //注册输入框监听通知
        NotificationCenter.default.addObserver(self, selector: #selector(textFieldTextChange(sender:)), name: .UITextFieldTextDidChange, object: textView)
        
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        self.addSubview(titleLabel)
        
        textView.borderStyle = .none
        textView.isSecureTextEntry = true
        textView.clearButtonMode = .always
        textView.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        self.addSubview(textView)
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.centerY.equalToSuperview()
            make.width.equalTo(RATIO(maxNum: 80))
            make.height.greaterThanOrEqualTo(10)
        }
        
        textView.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel.snp.right)
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.centerY.height.equalToSuperview()
        }
    }
    
    //MARK: 监听文本
    @objc fileprivate func textFieldTextChange(sender : Notification) {
        
        self.inputDelegate?.checkInputText!()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}
